import yaml
import cv2
import numpy as np
from PIL import Image
from io import BytesIO

# from ocr.utils.try_exception import error_handler1


def parse_yaml(path):
    with open(path, 'r') as f:
        data = yaml.load(f, Loader=yaml.FullLoader)

    return data


def read_image(image):
    img = cv2.imdecode(image, cv2.IMREAD_COLOR)
    if img is None:
        bytes_io = bytearray(image)
        img = np.array(Image.open(BytesIO(bytes_io)))
        if img is None:
            return None
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

    return img
